package de.sabram.upb.specs.eee_tools.contextMenuActions;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ListSelectionDialog;
import org.eclipse.ui.part.FileEditorInput;

public class GenerateSpecsFileDelegate implements IViewActionDelegate{

	public GenerateSpecsFileDelegate() {
	}
	@Override
	public void run(IAction action) {
		IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
		IProject currentProject = null;
		IProject [] projects = workspaceRoot.getProjects();
		for(IProject project : projects){
			if(project.exists() && project.isOpen())
				currentProject = project;
		}
		boolean fileExists = false;
		IPath pathToProject = currentProject.getLocation();
		File dir = pathToProject.toFile();
		FilenameFilter filter = new FilenameFilter(){
			public boolean accept(File dir, String name){
				return !name.startsWith(".") && !name.endsWith(".specs");
			}
		};
		if (currentProject.findMember(currentProject.getName() + ".specs") != null){
			fileExists = true;
		}
		String [] children = dir.list(filter);
		final ListSelectionDialog dialog = new ListSelectionDialog(
		        PlatformUI.getWorkbench().getDisplay().getActiveShell(),
		        children,
		        new ArrayContentProvider(), 
		        new LabelProvider(), 
		        "");		
		dialog.setTitle(".specs File Creation Wizard");
		dialog.setMessage("Select all files which should be added to the .specs file");
		dialog.setBlockOnOpen(true);
		dialog.open();
		Object[] results = dialog.getResult();
		IFile specsFile = currentProject.getFile(currentProject.getName() + ".specs");
		
		String content = "";
		for(Object result : results){
			content = content + result.toString() + "\n";
		}
		InputStream source = new ByteArrayInputStream(content.getBytes());
		try {
			if(fileExists)
				specsFile.setContents(source, false, false, null);
			else
				specsFile.create(source, false, null);
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IEditorDescriptor desc = PlatformUI.getWorkbench().
		        getEditorRegistry().getDefaultEditor(specsFile.getName());
		try {
			page.openEditor(new FileEditorInput(specsFile), desc.getId());
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
	}

	@Override
	public void init(IViewPart view) {
	}

}
